<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag,ElRow,ElCol,ElTree } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi } from '@/api/table'
import { useTable } from './web/vCircleTable'
import { TableData } from '@/views/vCircle/table/types.ts'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import { getVcirce , delectForum,tree} from '@/views/vCircle/table'
import { inject } from "vue";


defineOptions({
  name: 'ExamplePage'
})

const { push } = useRouter()

const { register, tableObject, methods } = useTable<TableData>({
  getListApi: getVcirce,
  delListApi: delTableListApi,
  response: {
    list: 'list',
    total: 'total'
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index'
  },
  {
    field: 'userName',
    label: t('page.bfusername')
  },
  {
    field: 'articleTitle',
    label: t('page.articleTitle')
  },
  {
    field: 'url',
    label: t('page.url'),
    sortable: true
  },
  {
    field: 'createDate',
    label: t('page.createDate'),
    sortable: true
  },
  {
    field: 'userAccount',
    label: t('page.account')
  },
  {
    field: 'articleLike',
    label: t('page.articleLike')
  },
  {
    field: 'articleHits',
    label: t('page.articleHits')
  },
  {
    field: 'articleReviews',
    label: t('page.articleReviews')
  },
  {
    field: 'articleShare',
    label: t('page.articleShare')
  },
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action')
  }
])
//树
const handleNodeClick = async(e) => {
  // const res = await getVcirce({actid:e.id})
  // tableObject.tableList=res.data
  tableObject.params=Object.assign(tableObject.params,{
    actid:e.id
  })
  getList()
}
let data=ref([{
  name:'请选择课程' ,
  children:[],
}]);

tree().then((res)=>{
  data.value[0].children=res.data
})

const defaultProps = {
  label: 'name',
  children: 'children',
}

const { allSchemas } = useCrudSchemas(crudSchemas)
//添加
const AddAction = () => {
  push('/example/example-add')
}
//批量删除
const delLoading = ref(false)

//局部刷新
const ceshi = inject("reload");


//行内删除
const delData = async (row: TableData | null, multiple: boolean) => {
  delectForum({"microcircleId" : row.microcircleId}).then((res) =>{
    if(res){
      getList()
    }
  })
}
//详情查询
const action = (row: TableData, type: string) => {
  push(`/external-link/lookVcircle3—${type}?microcircleId=${row.microcircleId}`)
}

//表单查询
var str = "name"
import { Form } from '@/components/Form'
import {FormSchema} from "@/types/form";
const schema = reactive<FormSchema[]>([
  {
    field: 'title',
    label: '文章名称',
    component: 'Input'
  },
  {
    field: 'name',
    label: '用户名称',
    component: 'Input'
  }
  ,{
    field: 'acc',
    label: '用户账号',
    component: 'Input'
  }
])

</script>

<template>
  <ElRow class="row-con">
    <ElCol :xl="3" :lg="3" :md="24" :sm="24" :xs="24">
      <ElTree :data="data" :props="defaultProps" @current-change="handleNodeClick" default-expand-all="false"/>
    </ElCol>
    <ElCol :xl="21" :lg="21" :md="24" :sm="24" :xs="24">
      <ContentWrap>
        <Search
                :expand = "true"
                :schema="schema"
                :expandField = str
                @search= "setSearchParams"
                @reset= "setSearchParams"
        />
        <Table
                v-model:pageSize="tableObject.pageSize"
                v-model:currentPage="tableObject.currentPage"
                :columns="allSchemas.tableColumns"
                :data="tableObject.tableList.slice((tableObject.currentPage-1) * tableObject.pageSize, tableObject.currentPage * tableObject.pageSize)"
                :loading="tableObject.loading"
                :pagination="{
        total: tableObject.total
      }"
                @register="register"
        >
          <template #action="{ row }">
            <ElButton type="success" @click="action(row, 'detail')">
              {{ t('详情') }}
            </ElButton>
            <ElButton type="danger" @click="delData(row, false)">
              {{ t('删除') }}
            </ElButton>
          </template>
        </Table>
      </ContentWrap>
    </ElCol>
  </ElRow>


</template>
